package com.niiwoo.civet.user.service.local;

import com.niiwoo.civet.user.dao.entity.NoticeMessage;
import com.niiwoo.civet.user.dao.entity.UserBasicInfo;
import com.niiwoo.civet.user.dao.mapper.NoticeMessageMapperExt;
import com.niiwoo.civet.user.dao.mapper.UserBasicInfoMapperExt;
import com.niiwoo.civet.user.dto.NoticeMessageDTO;
import com.niiwoo.tripod.base.component.PrivacyMasks;
import com.niiwoo.tripod.provider.exception.BizException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.curator.shaded.com.google.common.collect.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by dell on 2017/12/15.
 * Description：civet-parent
 */
@Slf4j
@Service
public class HomePageService {

    @Autowired
    private NoticeMessageMapperExt noticeMessageMapperExt;
    @Autowired
    private UserBasicInfoMapperExt userBasicInfoMapperExt;
    @Autowired
    private PrivacyMasks privacyMasks;

    /**
     * 获取最新公告id
     * @return
     */
    public Long newestNoticeId() {
        Integer noticeId = noticeMessageMapperExt.newestNoticeId();
        return (noticeId!=null) ? noticeId.longValue() : 0L;
    }


    /**
     * 查询首页公告列表
     * @param pageNumber
     * @param pageSize
     * @param userId
     * @return
     */
    public Map<String, Object> postNiiwooNoticeList(int pageNumber, int pageSize, String userId) {
		Map<String, Object> result = new HashMap<>();

		int offset = (pageNumber - 1) * pageSize;
		
		// 用户ID不为空的查询用 户手机号码
		String mobile = "";
		if (StringUtils.isNotEmpty(userId)) {
			UserBasicInfo userBasicInfo = userBasicInfoMapperExt.selectByUserIdForAsset(userId);
			if (Objects.nonNull(userBasicInfo)) {
				mobile = privacyMasks.decryptPrivacy(userBasicInfo.getMobileNo());
			}			
		}

        //查询总记录数
		int totalCount = noticeMessageMapperExt.queryNoticeListCount(mobile);
		int totalPage = (totalCount % pageSize > 0) ? (totalCount / pageSize + 1) : (totalCount / pageSize);
		result.put("totalCount", totalCount);
		result.put("totalPage", totalPage);
		
		// 数据为空直接返回
		if (totalCount == 0) {
			return result;
		}

		// 查询分页数据
		List<NoticeMessage> noticeMessages = noticeMessageMapperExt.queryNoticeListByPage(offset, pageSize, mobile);

		// 转化成DTO
		List<NoticeMessageDTO> noticeMessageDTOS = new ArrayList<>();
		for (NoticeMessage message : noticeMessages) {
			NoticeMessageDTO dto = new NoticeMessageDTO();
			BeanUtils.copyProperties(message, dto);
			noticeMessageDTOS.add(dto);
		}
		result.put("noticeList", noticeMessageDTOS);
        return result;
    }


    /**
     * 查看公告详情
     * @param id
     * @return
     */
    public NoticeMessageDTO getNoticeDetail(Long id) {
        NoticeMessage message = noticeMessageMapperExt.selectByPrimaryKey(id.intValue());
        if (message==null) throw new BizException("USR19071");
        NoticeMessageDTO dto = new NoticeMessageDTO();
        BeanUtils.copyProperties(message, dto);
        return dto;
    }
}
